package com.itheima.bos.web.action.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.itheima.bos.domain.base.Courier;
import com.itheima.bos.service.base.ICourierService;
import com.itheima.bos.web.action.common.BaseAction;

public class CourierAction extends BaseAction<Courier> {

	// 注入sevice
	@Autowired
	private ICourierService courierService;

	// 保存快递员信息操作
	@Action(value = "courier_sava", results = { @Result(name = "success", location = "/pages/base/courier.html", type = "redirect") })
	public String sava() {
		// 调用业务层，保存数据
		courierService.sava(model);
		return SUCCESS;
	}

	// 快递员分页查询操作
	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 封装pageable对象
		Pageable pageable = new PageRequest(page - 1, rows);

		// 根据条件构造查询对象
		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 存放条件查询对象
				List<Predicate> list = new ArrayList<Predicate>();
				// 单表查询对象
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					// 查询快递员工号查询
					Predicate p1 = cb.equal(
							root.get("courierNum").as(String.class),
							model.getCourierNum());
					// 将查询对象存放到list集合中
					list.add(p1);
				}

				if (StringUtils.isNotBlank(model.getCompany())) {
					// 进行公司查询,模糊查询
					Predicate p2 = cb.like(
							root.get("company").as(String.class),
							"%" + model.getCompany() + "%");
					list.add(p2);
				}

				if (StringUtils.isNotBlank(model.getType())) {
					// 快递员类型查询
					Predicate p3 = cb.equal(root.get("type").as(String.class),
							model.getType());
					list.add(p3);
				}

				// 多表查询
				// 关联对象
				Join<Object, Object> standardRoot = root.join("standard",
						JoinType.INNER);
				if (model.getStandard() != null
						&& StringUtils
								.isNotBlank(model.getStandard().getName())) {
					// 进行收派标准查询,模糊查询
					Predicate p4 = cb.like(root.get("name").as(String.class),
							"%" + model.getStandard().getName() + "%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};

		Page<Courier> pageData = courierService.findPageData(specification,
				pageable);

		// 返回json数据，将json数据押进栈顶
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	// 属性驱动
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "courier_deleBacth", results = { @Result(name = "success", location = "/pages/base/courier.html", type = "redirect") })
	public String deleBacth() {

		String[] idArray = ids.split(",");

		// 调用业务层，实现批量作废功能
		courierService.deleBacth(idArray);
		return SUCCESS;
	}

	@Action(value = "courier_findNoassociation", results = { @Result(name = "success", type = "json") })
	public String findNoassociation() {

		// 调用业务层查询未关联的快递员
		List<Courier> couriers = courierService.findNoassociation();
		//将数据压进栈
		pushPageDataToValueStack(couriers);
		return SUCCESS;
	}

}
